/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package lifeed;

import unito.likir.ca.CA;
import unito.likir.Environment;
import unito.likir.EnvironmentImpl;
import unito.likir.Node;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.io.IOException;

/**
 *
 * @author grillo
 */
public class StartEnv {

	private static InputStreamReader inReader = new InputStreamReader(System.in);
	private static BufferedReader bufReader = new BufferedReader(inReader);

    private static CA ca = null;
    private static Environment env = null;

    public static void main(String[] args)
	{
		while (true) {
			printEnvironmentMenu();
			int choice = choose(" >> ", 0, 9);
			switch (choice)
			{
                case 1 :
                    initialize();
                    break;
                case 2 :
                    System.out.println("*** STATUS CA ***");
                    System.out.println(ca);
                    //print the Storages: stored contents are maintained
                    System.out.println("*** STATUS STORAGE ***");
                    //System.out.println(env);
                    for (Node x : env.getAllNodes())
                        System.out.println(x.getStorage());
                    break;
				case 0:
					shutdown();
                    System.exit(0);
				default:
					break;
			}
		}
	}

    public static void initialize() {
		int nodeNumber = 4; //number of nodes in the network
		int initialPort = 8000; //initial UDP port
		String userNamePrefix = "User"; //userId common prefix

        try {
            ca = new CA();
            ca.startup();
        } catch (Exception e) {}

		/*
		 * PART I : Network initialization
		 */
		//Create the environment and initialize all the node
		env = new EnvironmentImpl();
		env.registerNodes(nodeNumber, userNamePrefix, initialPort); //Nodes are registered to the Environment
		env.startupAll(); //CA is contacted by every Node
		env.bootstrapAll(); //Kademlia bootstrap procedure is completed by every Node

        try {
			String path = "." + File.separator + "Contents" + File.separator + "user0_settings.xml";
            env.getAllNodes().iterator().next().put(new File(path), "settings", 360000);
        } catch(IOException e) {}
    }

    public static void shutdown() {
		//shutdown without save
		env.shutDownAll(false);
        ca.shutdown(false);
    }

	private static void printEnvironmentMenu()
	{
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append("\n+------------------------------------+\n");
		stringBuilder.append("|             ENVIRONMENT            |\n");
		stringBuilder.append("+------------------------------------+\n");
		stringBuilder.append("| 1 - Start Network                  |\n");
		stringBuilder.append("| 2 - Status Network                 |\n");
		stringBuilder.append("| 0 - Exit                           |\n");
		stringBuilder.append("+------------------------------------+");
		System.out.println(stringBuilder.toString());
	}

	private static int choose(String requestString, int min, int max)
	{
		String input;
		int inputNumber = -1;
		while (inputNumber <  min || inputNumber > max)
		{
			try
			{
				System.out.print(requestString);
				input = bufReader.readLine();
				inputNumber = Integer.parseInt(input);
			}
			catch(Exception e)
			{
				inputNumber = -1;
			}
		}
		return inputNumber;
	}
}
